<template>
  <nav class="navbar">
    <div class="logo">
      <div class="logo-icon">
        <img class="fas fa-dragon" src="../assets/logo.png" alt="Vue logo">
      </div>
      <div class="logo-text">校园冒险者公会</div>
    </div>

    <div class="nav-links" ref="navLinksRef">
      <a
          v-for="link in navLinks"
          :key="link.page"
          :class="['nav-link', { active: activePath === link.page }]"
          @click="setActiveNav(link.page, $event)"
          :data-page="link.page"
      >
        <i :class="link.icon"></i> {{ link.text }}
      </a>
      <!-- 下划线元素 -->
      <div class="nav-underline" :style="underlineStyle"></div>
    </div>

    <div class="nav-actions">
      <a-button class="nav-btn" type="text">
        <template #icon>
          <i class="fas fa-bell"></i>
        </template>
      </a-button>
      <a-button class="nav-btn" type="text">
        <template #icon>
          <i class="fas fa-cog"></i>
        </template>
      </a-button>
      <a-button class="nav-btn accent">
        <template #icon>
          <i class="fas fa-user"></i>
        </template>
        张同学
      </a-button>
    </div>
  </nav>
</template>

<script>
import { defineComponent, ref, onMounted, nextTick } from 'vue'
import { useRouter, useRoute } from 'vue-router'

export default defineComponent({
  name: 'NavBar',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const navLinksRef = ref(null)
    const activePath = ref('')
    const underlineStyle = ref({
      width: '0px',
      left: '0px'
    })

    const navLinks = [
      { page: 'home', text: '首页', icon: 'fas fa-home' },
      { page: 'tasks', text: '任务大厅', icon: 'fas fa-tasks' },
      { page: 'guild', text: '公会', icon: 'fas fa-users' },
      { page: 'profile', text: '个人中心', icon: 'fas fa-user' }
    ]

    // 更新下划线位置和宽度
    const updateUnderline = async (page) => {
      await nextTick()
      if (!navLinksRef.value) return

      const activeElement = navLinksRef.value.querySelector(`.nav-link[data-page="${page}"]`)
      if (activeElement) {
        const { offsetLeft, clientWidth } = activeElement
        underlineStyle.value = {
          width: `${clientWidth}px`,
          left: `${offsetLeft}px`
        }
      }
    }

    // 设置激活导航
    const setActiveNav = (page, event) => {
      event.preventDefault()
      activePath.value = page
      router.push(`/${page}`)
      updateUnderline(page)
    }

    // 初始化和路由监听
    onMounted(() => {
      // 从当前路由路径中提取页面标识
      const currentPage = route.path.split('/')[1] || 'home'
      activePath.value = currentPage
      updateUnderline(currentPage)

      // 监听路由变化
      router.afterEach((to) => {
        const newPage = to.path.split('/')[1] || 'home'
        activePath.value = newPage
        updateUnderline(newPage)
      })
    })

    return {
      navLinksRef,
      activePath,
      underlineStyle,
      navLinks,
      setActiveNav
    }
  }
})
</script>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25px 0;
  position: relative;
  z-index: 100;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  background: linear-gradient(135deg, #172a45 0%, #172a45 100%);
  width: 50px;
  height: 50px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(212, 175, 55, 0.3);
}

.logo-icon img {
  font-size: 24px;
  color: white;
  width: 50px;
  height: 50px;
}

.logo-text {
  font-family: 'Noto Serif SC', serif;
  font-weight: 700;
  font-size: 24px;
  background: linear-gradient(to right, #d4af37, #f9e076);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.nav-links {
  display: flex;
  gap: 35px;
  position: relative; /* 为下划线提供定位基准 */
}

.nav-link {
  color: #f0f4f8;
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  padding: 5px 0;
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  white-space: nowrap;
}

.nav-link:hover {
  color: #d4af37;
}

.nav-link.active {
  color: #d4af37;
}

/* 下划线样式 */
.nav-underline {
  position: absolute;
  bottom: 0;
  height: 2px;
  background: #d4af37;
  transition: all 0.3s ease; /* 平滑过渡效果 */
  box-shadow: 0 2px 8px rgba(212, 175, 55, 0.3);
}

.nav-actions {
  display: flex;
  gap: 20px;
  align-items: center;
}

.nav-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #f0f4f8;
  border-radius: 8px;
  backdrop-filter: blur(10px);
}

.nav-btn.accent {
  background: linear-gradient(135deg, #d4af37 0%, #b8972e 100%);
  border: none;
  box-shadow: 0 4px 20px rgba(212, 175, 55, 0.3);
}

.nav-btn.accent:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(212, 175, 55, 0.4);
}
</style>